package com.rongchuang.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.UserDTO;
import com.rongchuang.entity.User;
import com.rongchuang.entity.QUser;
import com.rongchuang.repository.UserRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import com.rongchuang.util.PasswordEncryptor;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * 分页查询
     */
    public Page<UserDTO> pageSearch(int pageNum, int pageSize, String username) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QUser user = QUser.user;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(username)) {
            builder.and(user.username.eq(username));
        }
        List<User> userList = queryFactory
                .selectFrom(user)
                .where(builder)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(user.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(user)
                .where(builder)
                .fetchCount();
        List<UserDTO> userDTOs = ConverterUtil.convertToList(userList, UserDTO.class);
        return new PageImpl<>(userDTOs, pageable, total);
    }

    /**
     * 保存管理员信息
     */
    public void modify(UserDTO dto) {
        String mdPassword = PasswordEncryptor.encryptPassword(dto.getPassword());
        User user;
        // 新增
        if (Objects.isNull(dto.getId())) {
            user = ConverterUtil.convertTo(dto, User.class);
            user.setPassword(mdPassword);
            user.setCreateTime(DateUtils.getCurrentTime());
            user.setStatus((byte) 1);
        }else {
            // 获取用户信息
            user = userRepository.getById(dto.getId());
            // 判断密码是否修改
            if (!user.getPassword().equals(dto.getPassword())) {
                user.setPassword(mdPassword);
            }
            if (StringUtils.isNotBlank(dto.getUsername())) {
                user.setUsername(dto.getUsername());
            }
            if (StringUtils.isNotBlank(dto.getMobile())) {
                user.setMobile(dto.getMobile());
            }
            if (Objects.nonNull(dto.getAssociationId())) {
                user.setAssociationId(dto.getAssociationId());
            }
            if (Objects.nonNull(dto.getStatus())) {
                user.setStatus(dto.getStatus());
            }
        }
        user.setUpdateTime(DateUtils.getCurrentTime());
        userRepository.save(user);
    }

    /**
     * 获取用户详情
     * @param id 用户id
     * @return 用户详情
     */
    public UserDTO getDetail(Long id) {
        User user = userRepository.getById(id);
        return ConverterUtil.convertTo(user, UserDTO.class);
    }

    /**
     * 更新状态
     */
    public void updateStatus(Long id, Byte status) {
        User user = userRepository.getById(id);
        user.setStatus(status);
        userRepository.save(user);
    }
}
